package com.example.percentframelayou;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //F:数据
    private String[] data = {"ban", "ban", "ban", "ban", "ban", "ban",
            "ban", "ban", "ban", "ban", "ban", "ban", "ban", "ban"};


    private List<Fruit> fruitList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();//初始水果数据


        //将自带的标题栏隐藏
//        ActionBar actionBar = getSupportActionBar();
//        if(actionBar != null){
//            actionBar.hide();
//        }


//F:
       //ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,data);
        FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
        ListView listView = findViewById(R.id.list_item);

        listView.setAdapter(adapter);

        //点击
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 Fruit fruit = fruitList.get(position);

                Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
            }
        });


    }


    //名字与图片对应
    private void initFruits() {
        for (int i = 0; i < 20; i++) {
            Fruit ban = new Fruit("ban", R.drawable.ic_launcher_background);
            fruitList.add(ban);
        }
    }

}